package de.fhb.itunes.controller.web;

import java.io.IOException;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import de.fhb.itunes.manager.Datenverwaltung.Albenverwaltung;
import de.fhb.itunes.manager.Datenverwaltung.Episodenverwaltung;
import de.fhb.itunes.manager.Datenverwaltung.Musikverwaltung;
import de.fhb.itunes.manager.Datenverwaltung.TVSendungsverwaltung;
import de.fhb.itunes.repository.data.Album_VO;
import de.fhb.itunes.repository.data.Episode_VO;
import de.fhb.itunes.repository.data.Musik_VO;
import de.fhb.itunes.repository.data.TvSendung_VO;

/**
 * Servlet implementation class tv
 */
@WebServlet("/tv")
public class tv extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public tv() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String nextJSP = "/login.jsp";
		HttpSession session = request.getSession(false);
		if (session != null) {
			Object objtmp = session.getAttribute("loggedIn");
			if (objtmp != null) {
				boolean isLoggedIn = (Boolean) objtmp;
				if (isLoggedIn) {// session.isNew()
					nextJSP = "/tv.jsp";
					Enumeration en = request.getParameterNames();
					int count = 0;
					String staffelname = "";
					String staffelnummer = "";
					int modus = -1;
					while (en.hasMoreElements()) {
						count++;
						String paramName = (String) en.nextElement();
						String paraValue = request.getParameter(paramName);
						if (paramName.contains("staffelname")) {
							staffelname = paraValue;
						} else if (paramName.contains("staffelnummer")) {
							staffelnummer = paraValue;
						} else if (paramName.contains("mode")) {
							modus = Integer.parseInt(paraValue);
						}
					}
					if (staffelname != "" && staffelnummer != "") {
						int nummer = -1;
						try{
							nummer = Integer.parseInt(staffelnummer);
							TVSendungsverwaltung tvs = new TVSendungsverwaltung();
							TvSendung_VO tv = tvs.getbyStaffelnameAndNummer(staffelname, nummer);
							
							request.setAttribute("tv-show", tv);
							
							nextJSP = "/tv_detail.jsp";
						} catch (Exception e){
							nextJSP = "/tv.jsp";
						}
					} else {
						TVSendungsverwaltung tvs = new TVSendungsverwaltung();
						List<TvSendung_VO> list = null;
						switch (modus) {
						case 1:
							list = tvs.getAll();
							request.setAttribute("tvListe", list);
							break;
						case 2:
							request.setAttribute("isSuche", true);
							break;
						case 3:
							List<Episode_VO> episodenlist = tvs.getLastAddedEpisodes(10);
							request.setAttribute("episodenliste", episodenlist);
							break;
						default:
							break;
						}
						

					}

				}
			}
		}
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher(nextJSP);
		dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String nextJSP = "/login.jsp";
		HttpSession session = request.getSession(false);
		if (session != null) {
			Object objtmp = session.getAttribute("loggedIn");
			boolean isLoggedIn;
			if (objtmp != null) {
				isLoggedIn = (Boolean) objtmp;
			} else {
				isLoggedIn = false;
			}
			if (isLoggedIn) {
				// logik anfang
				nextJSP = "/tv.jsp";
				Enumeration en = request.getParameterNames();
				String keyword = "";
				while (en.hasMoreElements()) {
					String paramName = (String) en.nextElement();
					String paraValue = request.getParameter(paramName);
					if (paramName.contains("keyword")) {
						keyword = paraValue;
					}
				}
				if (keyword != "") {
					TVSendungsverwaltung tv = new TVSendungsverwaltung();
					List<Episode_VO> episoden = new LinkedList<Episode_VO>();

					episoden = tv.searchbyKeyword(keyword);

					request.setAttribute("episodenliste", episoden);

				}

				// logik ende
			}
		} else
			nextJSP = "/login.jsp";
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher(nextJSP);
		dispatcher.forward(request, response);
	}

}
